﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameObjects;

namespace StillAlive
{
    /// <summary>
    /// Здесь находится процесс игры (не меню)
    /// </summary>
    public class Battlefield : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        // наш игрок
        List<DrawableCharacter> players;

        public Battlefield(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            players = new List<DrawableCharacter>();
            Texture2D playerTexture = Game.Content.Load<Texture2D>(@"Images\player");
            // управляемый персонаж 1
            DrawableCharacter player1 = new DrawableCharacter();
            player1.moveBehavior = new PlayerMove1(player1);
            player1.Position = new Vector2(200, 200);
            player1.Speed = 5;
            player1.Sprite = new Sprite(
                playerTexture, new Point(32, 32), 0, Point.Zero, new Point(1, 1));
            players.Add(player1);
            // управляемый персонаж 2
            DrawableCharacter player2 = new DrawableCharacter();
            player2.moveBehavior = new PlayerMove2(player2);
            player2.Position = new Vector2(200, 200);
            player2.Speed = 5;
            player2.Sprite = new Sprite(
                playerTexture, new Point(32, 32), 0, Point.Zero, new Point(1, 1));
            players.Add(player2);
            // монстер
            DrawableCharacter monster = new DrawableCharacter();
            monster.moveBehavior = new StupidMonsterMove(monster, player1, player2);
            monster.Position = new Vector2(300, 300);
            monster.Speed = 1;
            monster.Sprite = new Sprite(
                playerTexture, new Point(32, 32), 0, Point.Zero, new Point(1, 1));
            players.Add(monster);
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (var player in players)
            {
                player.Update(gameTime);
                player.Move();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            // рисуем игрока
            foreach (var character in players)
            {
                character.Draw(gameTime, spriteBatch);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
